package de.zainodis.triplog.util;

import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Environment;
import android.preference.PreferenceManager;

import java.io.File;

import de.zainodis.triplog.R;

import static junit.framework.Assert.assertTrue;

/**
 * General-purpose utility class.
 * Created by fzarrai on 8/21/2014.
 */
public class Utilities {

    public static final String APP_DIRECTORY = "Triplog";

    /**
     * @param osmIconId a two digit number as defined from OSM (icon names).
     * @return the matching drawable resource id for the given osm icon id.
     */
    public static int getWeatherIconId(int osmIconId) {
        switch (osmIconId) {
            case 1:
                return R.drawable.ic_clear_sky;
            case 2:
                return R.drawable.ic_few_clouds;
            case 3:
                return R.drawable.ic_scattere_clouds;
            case 4:
                return R.drawable.ic_broken_clouds;
            case 9:
                return R.drawable.ic_shower_rain;
            case 10:
                return R.drawable.ic_rain;
            case 11:
                return R.drawable.ic_thunderstorm;
            case 13:
                return R.drawable.ic_snow;
            case 50:
                return R.drawable.ic_mist;
        }
        return 0;
    }

    /**
     * @param context context required to access shared preferences.
     * @return the preferred location string of the user; null if none has been set or the given context is null.
     */
    public static String getPreferredLocation(Context context) {
        if (context == null) {
            return null;
        }
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        String location = preferences.getString(context.getString(R.string.pref_key_weather_location), null);
        return location;
    }

    /**
     * @param context context required to access shared preferences.
     * @return the preferred unit; null if the given context is null; metric system if no preference has been set.
     */
    public static String getPreferredUnit(Context context) {
        if (context == null) {
            return null;
        }
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        String unit = preferences.getString(context.getString(R.string.pref_key_weather_units), context.getString(R.string.pref_value_metric));
        return unit;

    }


    public static int fahrenheitToCelsius(int degrees) {
        return Math.round((degrees - 32) * 5 / 9);
    }

    public static int celsiusToFahrenheit(int degrees) {
        return Math.round((degrees * 9) / 5 + 32);
    }

    public static String formatTemperature(Context context, int temperature, boolean isMetric) {
        if (!isMetric) {
            //Convert to imperial
            temperature = celsiusToFahrenheit(temperature);
        }

        return context.getString(R.string.temperature_format, temperature);
    }

    public static float mphToKmh(float mph) {
        return mph * 1.609f;
    }

    public static float kmhToMph(float kmh) {
        return kmh * 0.621f;
    }

    /**
     * @param values the content values from which to extract a value using the given key.
     * @param key    key via which the value is retrieved.
     * @return a long that is mapped to the given key; 0 otherwise.
     */
    public static long getAsLong(ContentValues values, String key) {
        if (values != null && values.containsKey(key)) {
            return values.getAsLong(key);
        }
        return 0;
    }

    /**
     * @param values the content values from which to extract a value using the given key.
     * @param key    key via which the value is retrieved.
     * @return a String that is mapped to the given key; null otherwise.
     */
    public static String getAsString(ContentValues values, String key) {
        if (values != null && values.containsKey(key)) {
            return values.getAsString(key);
        }
        return null;
    }

    /**
     * @param values the content values from which to extract a value using the given key.
     * @param key    key via which the value is retrieved.
     * @return a double that is mapped to the given key; 0 otherwise.
     */
    public static double getAsDouble(ContentValues values, String key) {
        if (values != null && values.containsKey(key)) {
            return values.getAsDouble(key);
        }
        return 0;
    }

    /**
     * @param values the content values from which to extract a value using the given key.
     * @param key    key via which the value is retrieved.
     * @return an integer that is mapped to the given key; 0 otherwise.
     */
    public static int getAsInteger(ContentValues values, String key) {
        if (values != null && values.containsKey(key)) {
            return values.getAsInteger(key);
        }
        return 0;
    }

    /**
     * @param values the content values from which to extract a value using the given key.
     * @param key    key via which the value is retrieved.
     * @return a float that is mapped to the given key; 0 otherwise.
     */
    public static float getAsFloat(ContentValues values, String key) {
        if (values != null && values.containsKey(key)) {
            return values.getAsFloat(key);
        }
        return 0;
    }

    /**
     * @return Directory where photos are saved. If it does not already exist, it will be created.
     */
    public static File getPhotoDirectory() {
        File imagesFolder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), APP_DIRECTORY);
        if (!imagesFolder.exists()) {
            assertTrue("Failed to create photo directory.", imagesFolder.mkdirs());
        }
        return imagesFolder;
    }
}
